package com.ruoyi.system.controller;

import com.doudian.open.api.order_batchDecrypt.OrderBatchDecryptRequest;
import com.doudian.open.api.order_batchDecrypt.OrderBatchDecryptResponse;
import com.doudian.open.api.order_batchDecrypt.data.DecryptInfosItem;
import com.doudian.open.api.order_batchDecrypt.param.CipherInfosItem;
import com.doudian.open.api.order_batchDecrypt.param.OrderBatchDecryptParam;
import com.doudian.open.api.order_orderDetail.OrderOrderDetailRequest;
import com.doudian.open.api.order_orderDetail.OrderOrderDetailResponse;
import com.doudian.open.api.order_orderDetail.data.ShopOrderDetail;
import com.doudian.open.api.order_orderDetail.data.SpecItem;
import com.doudian.open.api.order_orderDetail.param.OrderOrderDetailParam;
import com.doudian.open.core.AccessToken;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.config.DouDianToken;
import com.ruoyi.system.domain.RxOrder;
import com.ruoyi.system.mapper.RxOrderMapper;
import com.ruoyi.system.utils.NumberUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import java.util.*;

@RestController
@RequestMapping("/hmk/douDian")
@Slf4j
public class DouDianDecrypt {

    @Autowired
    private RxOrderMapper rxOrderMapper;

    @PermitAll
    @PostMapping("/decrypt")
    public AjaxResult decrypt(@RequestBody Map<String, String> params) {
        String douDianOrderId = params.get("douDianOrderId");
        String encrypt = params.get("encrypt");
        OrderBatchDecryptRequest oBDRequest = new OrderBatchDecryptRequest();
        OrderBatchDecryptParam oBDParam = new OrderBatchDecryptParam();
        List<CipherInfosItem> cipherInfosItems = new LinkedList<>();
        CipherInfosItem cipherInfosItem1 = new CipherInfosItem();
        cipherInfosItem1.setAuthId(douDianOrderId);
        cipherInfosItem1.setCipherText(encrypt);
        cipherInfosItems.add(cipherInfosItem1);
        oBDParam.setCipherInfos(cipherInfosItems);
        oBDRequest.setParam(oBDParam);
        return AjaxResult.success();
    }

    @PermitAll
    @PostMapping("/decryptByDouDianOrderId")
    public AjaxResult decryptByDouDianOrderId(@RequestBody Map<String, String> params) {
        String douDianOrderId = params.get("douDianOrderId");
        String shopId = params.get("shopId");
        AccessToken accessToken;
        accessToken = DouDianToken.getToken(shopId);
        OrderOrderDetailRequest orderDetailRequest = new OrderOrderDetailRequest();
        OrderOrderDetailParam param2 = orderDetailRequest.getParam();
        param2.setShopOrderId(douDianOrderId);
        OrderOrderDetailResponse response = orderDetailRequest.execute(accessToken);
        String spec3 = "";
        ShopOrderDetail shopOrderDetail = response.getData().getShopOrderDetail();
        List<SpecItem> spec = shopOrderDetail.getSkuOrderList().get(0).getSpec();
        if (null != spec && spec.size() == 3) {
            String sellerTel = NumberUtils.PhoneRemoveSpaces(spec.get(2).getValue());
            spec3 = sellerTel;
            if (sellerTel.length() < 11) {
                Random random = new Random();
                long random10Digit = 1000000000L + (long) (random.nextDouble() * 9000000000L);
                sellerTel = "A" + random10Digit;
            } else if (sellerTel.length() > 11) {
                sellerTel = sellerTel.substring(0, 11);
                spec3 = sellerTel;
            }
            shopOrderDetail.setSellerWords(sellerTel);
        } else {
            log.info("tag110商品sku配置不对不是3个值,值是:{},直接将当前msg2表状态修改为2,抖店商品id:{}", spec.size(), douDianOrderId);
            return AjaxResult.error();
        }


        RxOrder newOrder = new RxOrder();
        newOrder.setDoudianOrderId(douDianOrderId);
        Map<String, String> map = new HashMap<>();
        OrderBatchDecryptRequest oBDRequest = new OrderBatchDecryptRequest();
        OrderBatchDecryptParam oBDParam = new OrderBatchDecryptParam();
        List<CipherInfosItem> cipherInfosItems = new LinkedList<>();
        CipherInfosItem cipherInfosItem1 = new CipherInfosItem();
        cipherInfosItem1.setAuthId(douDianOrderId);
        cipherInfosItem1.setCipherText(shopOrderDetail.getEncryptPostReceiver());
        cipherInfosItems.add(cipherInfosItem1);
        map.put(shopOrderDetail.getEncryptPostReceiver(), "postReceiver");
        CipherInfosItem cipherInfosItem2 = new CipherInfosItem();
        cipherInfosItem2.setAuthId(douDianOrderId);
        cipherInfosItem2.setCipherText(shopOrderDetail.getEncryptPostTel());
        cipherInfosItems.add(cipherInfosItem2);
        map.put(shopOrderDetail.getEncryptPostTel(), "postTel");
        CipherInfosItem cipherInfosItem3 = new CipherInfosItem();
        cipherInfosItem3.setAuthId(douDianOrderId);
        cipherInfosItem3.setCipherText(shopOrderDetail.getPostAddr().getEncryptDetail());
        cipherInfosItems.add(cipherInfosItem3);
        map.put(shopOrderDetail.getPostAddr().getEncryptDetail(), "addrDetail");
        CipherInfosItem cipherInfosItem4 = new CipherInfosItem();
        cipherInfosItem4.setAuthId(douDianOrderId);
        cipherInfosItem4.setCipherText(shopOrderDetail.getUserIdInfo().getEncryptIdCardName());
        cipherInfosItems.add(cipherInfosItem4);
        map.put(shopOrderDetail.getUserIdInfo().getEncryptIdCardName(), "cardName");
        CipherInfosItem cipherInfosItem5 = new CipherInfosItem();
        cipherInfosItem5.setAuthId(douDianOrderId);
        cipherInfosItem5.setCipherText(shopOrderDetail.getUserIdInfo().getEncryptIdCardNo());
        cipherInfosItems.add(cipherInfosItem5);
        map.put(shopOrderDetail.getUserIdInfo().getEncryptIdCardNo(), "cardNo");
        oBDParam.setCipherInfos(cipherInfosItems);
        shopOrderDetail.getMaskPostAddr().getProvince();
        shopOrderDetail.getMaskPostReceiver();
        oBDRequest.setParam(oBDParam);
        log.info("{}手动调解密前:{}", douDianOrderId, oBDRequest);
        OrderBatchDecryptResponse oBDResponse = oBDRequest.execute(accessToken);
        log.info("{}手动调解密后:{}", douDianOrderId, oBDResponse);
        List<DecryptInfosItem> decryptInfos = oBDResponse.getData().getDecryptInfos();
        if ("10000".equals(oBDResponse.getCode())) {
            for (DecryptInfosItem decrypt : decryptInfos) {
                String value = map.get(decrypt.getCipherText());
                switch (value) {
                    case "postReceiver":
                        newOrder.setContactname(decrypt.getDecryptText());
                        break;
                    case "postTel":
                        newOrder.setContacttel(decrypt.getDecryptText());
                        newOrder.setCustomTel(decrypt.getDecryptText());
                        break;
                    case "addrDetail":
                        newOrder.setRealaddress(decrypt.getDecryptText());
                        newOrder.setCustomAddress(decrypt.getDecryptText());
                        break;
                    case "cardName":
                        newOrder.setCustomName(decrypt.getDecryptText());
                        break;
                    case "cardNo":
                        newOrder.setCustomNumber(decrypt.getDecryptText());
                        break;
                }
            }
            RxOrder oldOrder = rxOrderMapper.selectRxOrderByDouDianId(douDianOrderId);
            if (null != oldOrder) {
                newOrder.setId(oldOrder.getId());
                rxOrderMapper.updateRxOrder(newOrder);
            }
            return AjaxResult.success(newOrder);
        } else {
            return AjaxResult.error("解密不是全部成功过,请查询服务器日志");
        }
    }
}
